﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Top.Api.Domain;
using Top.Api.Request;
using Top.Api.Response;
using Top.Api;

namespace KSGJ
{
    public partial class PromotionCreate : System.Web.UI.Page
    {
        private static string appkey = System.Configuration.ConfigurationManager.AppSettings["AppKey"].ToString();
        private static string appsecret = System.Configuration.ConfigurationManager.AppSettings["AppSecret"].ToString();
        private static string appurl = System.Configuration.ConfigurationManager.AppSettings["AppUrl"].ToString();
        private static ITopClient client = new DefaultTopClient(appurl, appkey, appsecret);
        private int checkcount = 0;


        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                Getgoodlist(null);
            }
            ScriptManager.RegisterStartupScript(this, this.GetType(), "resizeImg", "<script>resizeImg()</script>", false);
            ScriptManager.RegisterStartupScript(this, this.GetType(), "onloadfun", "<script>onloadfun();</script>", false);
        }

        private void Getgoodlist(string cid, long pageno = 1)
        {
            ItemsOnsaleGetRequest onsellreq = new ItemsOnsaleGetRequest();
            onsellreq.Fields = "num_iid,title,price,pic_url";
            onsellreq.PageSize = this.AspNetPager.PageSize;
            onsellreq.PageNo = pageno;
            if (cid != null)
            {
                onsellreq.SellerCids = cid;
            }
            ItemsOnsaleGetResponse onSellRes = client.Execute(onsellreq, Session["sessionKey"].ToString());
            this.goodlist.DataSource = onSellRes.Items;
            this.goodlist.DataBind();
            this.AspNetPager.RecordCount = int.Parse(onSellRes.TotalResults.ToString());
        }

        protected void goodlist_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
            {
                Item gooditem = e.Item.DataItem as Item;
                Label opl = e.Item.FindControl("haspromotion") as Label;
                HiddenField hid = e.Item.FindControl("Hiddennid") as HiddenField;
                if (this.goodslist.Value.IndexOf(gooditem.NumIid + ",") >= 0)
                {
                    System.Web.UI.HtmlControls.HtmlInputCheckBox cbx = e.Item.FindControl("subcheckbox") as System.Web.UI.HtmlControls.HtmlInputCheckBox;
                    cbx.Checked = true;
                    System.Web.UI.HtmlControls.HtmlGenericControl cblabel = e.Item.FindControl("sublabel") as System.Web.UI.HtmlControls.HtmlGenericControl;
                    cblabel.Attributes["class"] = "checkbox checkbox-checked";
                    System.Web.UI.HtmlControls.HtmlControl gli = e.Item.FindControl("goodli") as System.Web.UI.HtmlControls.HtmlControl;
                    gli.Attributes.Add("class", "selectli");
                    checkcount += 1;
                }
                if (checkcount == this.AspNetPager.PageSize)
                {
                    this.allcheck.Checked = true;
                    this.alllabel.Attributes["class"] = "checkbox checkbox-checked";
                }
                else
                {
                    this.allcheck.Checked = false;
                    this.alllabel.Attributes["class"] = "checkbox";
                }

                if (Request.QueryString["ptype"] == "0")
                {
                    double dp7 = Double.Parse(gooditem.Price) * 0.7;
                    double dp1 = Double.Parse(gooditem.Price) - Double.Parse(Request.QueryString["pvalue"]);
                    System.Web.UI.HtmlControls.HtmlGenericControl afterp = e.Item.FindControl("afterprice") as System.Web.UI.HtmlControls.HtmlGenericControl;
                    afterp.InnerText = dp1.ToString();
                    if (dp1 < dp7)
                    {
                        opl.Visible = true;
                        opl.Text = "优惠后价格小于原价的7折";
                        afterp.Attributes.Add("style", "color:red");
                        hid.Value += "|0";
                    }
                }
                else
                {
                    double dp2 = Double.Parse(gooditem.Price) * (Double.Parse(Request.QueryString["pvalue"]) * 0.1);
                    System.Web.UI.HtmlControls.HtmlGenericControl afterp = e.Item.FindControl("afterprice") as System.Web.UI.HtmlControls.HtmlGenericControl;
                    afterp.InnerText = dp2.ToString();
                }
                if (!opl.Visible)
                {
                    MarketingPromotionsGetRequest promotionget = new MarketingPromotionsGetRequest();
                    promotionget.Fields = "promotion_id,start_date,end_date,status,tag_id,discount_value,discount_type";
                    promotionget.NumIid = gooditem.NumIid.ToString();
                    promotionget.Status = "ACTIVE";
                    promotionget.TagId = 1;
                    MarketingPromotionsGetResponse promotionresp = client.Execute(promotionget, Session["sessionKey"].ToString());
                    if (!promotionresp.IsError)
                    {
                        if (promotionresp.Promotions.Count > 0)
                        {
                            opl.Visible = true;
                            hid.Value += "|1";
                        }
                    }
                }
            }
        }

        protected void AspNetPager_PageChanging(object src, Wuqi.Webdiyer.PageChangingEventArgs e)
        {
            checkcount = 0;
            if (ViewState["searchkeyword"] != null)
            {
                //bindsearch(e.NewPageIndex);
            }
            else
            {
                //GetTradelist(this.AspNetPager.CurrentPageIndex);
                Getgoodlist(null, e.NewPageIndex);
            }
        }
    }
}